<?php

namespace iBrand\Shop\Core\Models;

use iBrand\Component\User\Models\User as BaseUser;
use iBrand\Component\Point\Traits\PointTrait;
use Illuminate\Support\Str;
use Laravel\Passport\HasApiTokens;

class User extends BaseUser
{
    use HasApiTokens, PointTrait;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setTable('el_user');
    }

    public function getAvatarAttribute($value)
    {
        if (!empty($value)) {
            if (str_contains($value, 'http://wx.qlogo.cn')) {
                return str_replace('http://wx.qlogo.cn', 'https://wx.qlogo.cn', $value);
            }

            return url($value);
        }

        return $value;
    }

    public function getNickNameAttribute($value)
    {
        if (empty($value)) {
            return $value;
        }

        if (Str::contains($value, 'base64:')) {
            return base64_decode(str_replace('base64:', '', $value));
        }

        return $value;
    }

    public function setNickNameAttribute($value)
    {
        $this->attributes['nick_name'] = 'base64:' . base64_encode($value);
    }

    public function getActionButtonsAttribute()
    {
        return $this->getEditButtonAttribute();
    }

    public function getEditButtonAttribute()
    {
//        if (Entrust::hasRole(['super_admin','admin']))
        return '<a href="' . route('admin.users.edit', $this->id) . '" class="btn btn-xs btn-primary"><i class="fa fa-pencil" data-toggle="tooltip" data-placement="top" title="' . '编辑' . '"></i></a> ';
//        return '';
    }

    public function shopUser()
    {
        return $this->hasOne(UserShop::class)->withDefault();
    }
}